package com.md.livingstreamsound.ui

import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.util.Log
import android.view.KeyEvent
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter
import androidx.viewpager.widget.ViewPager
import com.alibaba.android.arouter.launcher.ARouter
import com.living.player.BottomPlayerFragment
import com.md.album.impl.FragmentServiceImpl
import com.md.album.interfaces.FragmentServiceListner
import com.md.basedpc.permission.PermissionHelper
import com.md.basedpc.permission.PermissionStatus
import com.md.basedpc.permission.listener.OnPermissionListener
import com.md.livingstreamsound.R
import com.md.livingstreamsound.databinding.ActivityMainBinding
import com.md.livingwidget.CoreBaseActivity
import com.md.mainpage.interfaces.IMainInfo
import com.md.mainpage.ui.MainPageFragment
import com.md.mymusic.MyMusicFragment

/**
 * @author liyue
 * created 2021/2/15
 * desc 作为主页面，内部由ViewPager+FragmentPagerAdapter组成
 * 遗留问题：
 * 1. 每日必听向上滑动时分类应该跟着滑上去，以后再做
 * 2. RecyclerView应该配合DiffUtil局部刷新的，以后再做
 */
private const val TAG="MainActivityKt"
//首页在ViewPager中的position
private const val MAIN_PAGE_POS=0;
//首页在ViewPager中的position
private const val MINE_POS=1
class MainActivityKt: CoreBaseActivity(),Runnable, ViewPager.OnPageChangeListener,View.OnClickListener ,IMainInfo, OnPermissionListener , FragmentServiceListner {

    private lateinit var binding:ActivityMainBinding
    private val fragments = ArrayList<Fragment>()
    private lateinit var mainAdapter: MainAdapter
    private lateinit var fragmentService: FragmentServiceImpl
    private lateinit var playerContainer:FrameLayout
    private var isShowSearch=false
    //防止关闭Activity后Handler还未执行完造成内存泄露
    companion object{
        @JvmStatic
        private val mHandler= Handler(Looper.getMainLooper())
        @JvmStatic
        private var isExit=false
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(LayoutInflater.from(this))
        val rootView = binding.root
        setContentView(rootView)
        initView()
        initPlayer()
        PermissionHelper.requestNecessaryPermission(this,true)
        setStatusBarColor(false, resources.getColor(com.md.livingwidget.R.color.white))
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if(isShowSearch){
          val mainFragment =  fragments[0] as MainPageFragment
            mainFragment.onBackPressed()
            return false
        }
        if (keyCode == KeyEvent.KEYCODE_BACK){
            if(!isExit) {
                isExit=true
                Toast.makeText(this,getString(R.string.living_tap_exit),Toast.LENGTH_SHORT).show()
                mHandler.postDelayed(this,1500)
            }
            else finish()
        }

        return false
    }

    override fun run() {
        isExit=false
    }
    override fun onPageScrollStateChanged(state: Int) {
    }

    override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
    }

    override fun onPageSelected(position: Int) {
        if (position==MAIN_PAGE_POS) {
            selectMainPage(true)
            selectMine(false)
        }else{
            selectMainPage(false)
            selectMine(true)
        }
        Log.e(TAG, "pos=$position")
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.lltMainBtn -> {
                binding.viewPager.setCurrentItem(MAIN_PAGE_POS,true)
                selectMainPage(true)
                selectMine(false)
            }
            R.id.lltMineBtn -> {
                binding.viewPager.setCurrentItem(MINE_POS,true)
                selectMainPage(false)
                selectMine(true)
            }
        }
    }

    override fun getSearchStatus(isShowMain: Boolean) {
        isShowSearch=!isShowMain
    }

    private fun initView(){
        val mainFrt=MainPageFragment()
        mainFrt.setIMainInfo(this)
        fragments.add(mainFrt)
        fragments.add(MyMusicFragment())
//        fragments.add(Fragment4())

        mainAdapter= MainAdapter(supportFragmentManager,fragments)
        binding.viewPager.adapter=mainAdapter
        binding.viewPager.setCanScroll(true)
        binding.rootContainer.setViewPager(binding.viewPager)
        binding.viewPager.addOnPageChangeListener(this)
        selectMainPage(true)
        selectMine(false)

        binding.lltMineBtn.setOnClickListener(this)
        binding.lltMainBtn.setOnClickListener(this)
        fragmentService= ARouter.getInstance().build("/app/addfragment").navigation() as FragmentServiceImpl
        fragmentService.setListner(this)

    }
    private fun initPlayer(){
       var transaction=  supportFragmentManager.beginTransaction()
        var fragment = BottomPlayerFragment()
        transaction.add(R.id.playerContainer,fragment)
        transaction.commit()
    }

    private inline fun selectMainPage(isSel: Boolean ){
        binding.ivMainpageBtn.isSelected=isSel
        binding.tvMainpageTitle.setTextColor(if (isSel)resources.getColor(R.color.colorAccent) else resources.getColor(R.color.living_262626))

    }

    private inline fun selectMine(isSel: Boolean){
        binding.ivMineBtn.isSelected=isSel
        binding.tvMineTitle.setTextColor(if (isSel)resources.getColor(R.color.colorAccent) else resources.getColor(R.color.living_262626))
    }

    class MainAdapter:FragmentPagerAdapter{
        private var fragmentList: List<Fragment> = ArrayList()

        constructor(fm:FragmentManager,fragmentList: List<Fragment>):super(fm){
            this.fragmentList=fragmentList
        }

        override fun getItem(position: Int): Fragment {
            return fragmentList[position]
        }

        override fun getCount(): Int {
           return fragmentList.size
        }

    }

    override fun onPermissionGranted(permissions: Array<out PermissionStatus>?) {

    }

    override fun onPermissionDeniedImmediate(permissions: Array<out PermissionStatus>?) {

    }

    override fun onPermissionDenied(permissions: Array<out PermissionStatus>?) {

    }

    override fun addFragment(fragment: Fragment) {
//        fragments.add(fragment)
//        mainAdapter.notifyDataSetChanged()
//        binding.viewPager.currentItem= fragments.size-1
    }


}